<?php
/**
 * 考情统计查询
 * Enter description here ...
 * @author cc
 * index     个人考勤
 * leave     请假
 * travel    出差
 * daysoff   调休
 */
namespace Home\Controller;
use Think\Controller;
class AttendanceController extends CommonController{
	public function index(){//个人考勤
		$datetime = $_POST['datetime']?$_POST['datetime']:date('Y-m');
		if(!is_ym($datetime)){ $this->error("非法日期时间"); }
		
		$user_name = session('user_name');
		$user = M("sys_user");
		$user_id = $user->where("user_name='$user_name'")->getField('user_id');
		$checkinout = M("checkinout");
		$checktime = $checkinout->where('user_id='.$user_id." AND date_format(checktime,'%Y-%m')='".$datetime."'")->order('checktime desc')->select();
//		echo $checkinout->getLastSql();
		$days = array('01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31');
		$list = array();
		$list2 = array();
//		dump($checktime);
		foreach ($days as $key=>$value){
			foreach ($checktime as $key1=>$value1){
			$day = date('d',strtotime($value1['checktime']));
				if($value == $day){
					$list[$day][]=$value1['checktime'];//将同一天的数据放到一个数组中
					$list2[]=$day;
				}
			}	
		}
//		dump($list);
		$data = array();
		foreach ($days as $key=>$value){
			if(in_array($value, $list2)){
				foreach ($list as $key2=>$value2){
					if($value == $key2){
						$data[$value]['date'] = $datetime."-".$value;
						$data[$value]['starttime'] = substr(end($value2),11,5);
						$data[$value]['endtime'] = substr($value2['0'],11,5);
					}
				}
			}
		}
		$this->assign('data',$data);
		$this->assign('datetime',$datetime);
		$this->assign("empty", emptyhtml("list"));
		$this->display();
	}
	
	public function lists(){
		$datetime = $_POST['datetime']?$_POST['datetime']:date('Y-m');
		if(!is_ym($datetime)){ $this->error("非法日期时间"); }
		$user_id = $_POST['user_id'];
		
		$user = M("sys_user");
		//查询出所有的用户
		$data_user = $user->where("status=1 and user_name != 'admin'")->order("user_name")->getField('id,user_id,name');
		if(empty($user_id)){
			$user_name = session('user_name');
			$user_id = $user->where("user_name='$user_name'")->getField('user_id');
		}
		$name = $user->where('user_id='.$user_id)->getField('name');
		$checkinout = M("checkinout");
		$checktime = $checkinout->where('user_id='.$user_id." AND date_format(checktime,'%Y-%m')='".$datetime."'")->order('checktime desc')->select();
		$days = array('01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31');
		$list = array();
		$list2 = array();
		foreach ($days as $key=>$value){
			foreach ($checktime as $key1=>$value1){
			$day = date('d',strtotime($value1['checktime']));
				if($value == $day){
					$list[$day][]=$value1['checktime'];//将同一天的数据放到一个数组中
					$list2[]=$day;
				}
			}	
		}
		
		$data = array();
		foreach ($days as $key=>$value){
			if(in_array($value, $list2)){
				foreach ($list as $key2=>$value2){
					if($value == $key2){
						$data[$value][date] = $datetime."-".$value;
						$data[$value][starttime] = substr(end($value2),11,5);
						$data[$value][endtime] = substr($value2['0'],11,5);
					}
				}
			}
		}
		$this->assign('name',$name);
		$this->assign('user_id',$user_id);
		$this->assign('data_user',$data_user);
		$this->assign('data',$data);
		$this->assign('datetime',$datetime);
		$this->assign("empty", emptyhtml("list"));
		$this->display();
	}
	
	public function leave(){//请假统计
		$model = M();
		$datetime = $_POST['datetime']?$_POST['datetime']:date('Y-m');
		if(!is_ym($datetime)){ $this->error("非法日期时间"); }
		$sql2 = "select data.data_value from oa_workflow_work work, oa_workflow_work_prcs prcs, oa_workflow_work_data data where work.type_id=1 and work.work_id=prcs.work_id and prcs.proc_no=0 and date_format(work.work_time,'%Y-%m')='$datetime' and data.work_id=work.work_id";
		$data2 = $model->query($sql2);
//		dump($data2);
		if(!empty($data2)){
			foreach ($data2 as $key2=>$value2){
				$data_value2 = explode('***', $value2['data_value']);
				$data2[$key2]['name'] = $data_value2['1'];
				$data2[$key2]['dept_name'] = $data_value2['2'];
				$data2[$key2]['send_time'] = $data_value2['0'];
				$data2[$key2]['start_time'] = $data_value2['15'];
				$data2[$key2]['end_time'] = $data_value2['16'];
				$data_value2['17'] = $data_value2['17'] ? $data_value2['17'] : 0;
				$data_value2['18'] = $data_value2['18'] ? $data_value2['18'] : 0;
				$data2[$key2]['count_time'] = $data_value2['17'].'天 '.$data_value2['18'].' 小时';
				$data2[$key2]['yuanyin'] = $data_value2['9'];
			}			
		}
		$this->assign("datetime", $datetime);
		$this->assign('data',$data2);
		$this->assign("empty", emptyhtml("list"));
		$this->display();
	}
	
	public function travel(){//出差统计
		$datetime = $_POST['datetime']?$_POST['datetime']:date('Y-m');
		if(!is_ym($datetime)){ $this->error("非法日期时间"); }
		$sql = "select data.data_value from oa_workflow_work work, oa_workflow_work_prcs prcs, oa_workflow_work_data data where work.type_id=3 and work.work_id=prcs.work_id and prcs.proc_no=0 and date_format(work.work_time,'%Y-%m')='$datetime' and data.work_id=work.work_id";
		$model = M();
		$data1 = $model->query($sql);
		if(!empty($data1)){
			foreach ($data1 as $key1=>$value1){
				$data_value1 = explode('***', $value1['data_value']);
				$data1[$key1]['name'] = $data_value1['1'];
				$data1[$key1]['dept_name'] = $data_value1['2'];
				$data1[$key1]['send_time'] = $data_value1['0'];
				$data1[$key1]['start_time'] = $data_value1['7'];
				$data1[$key1]['end_time'] = $data_value1['8'];
				$data1[$key1]['count_time'] = $data_value1['3'];
				$data1[$key1]['start_add'] = $data_value1['5'];
				$data1[$key1]['end_add'] = $data_value1['6'];
			}
		}
		$this->assign("datetime", $datetime);
		$this->assign('data1',$data1);
		$this->assign("empty", emptyhtml("list"));
		$this->display();
	}
	
	public function daysoff(){//调休统计
		$model = M();
		$datetime = $_POST['datetime']?$_POST['datetime']:date('Y-m');
		if(!is_ym($datetime)){ $this->error("非法日期时间"); }
		$sql3 = "select data.data_value from oa_workflow_work work, oa_workflow_work_prcs prcs, oa_workflow_work_data data where work.type_id=11 and work.work_id=prcs.work_id and prcs.proc_no=0 and date_format(work.work_time,'%Y-%m')='$datetime' and data.work_id=work.work_id";
		$data3 = $model->query($sql3);
//		dump($data3);
		if(!empty($data3)){
			foreach ($data3 as $key3=>$value3){
				$data_value3 = explode('***', $value3['data_value']);
				$data3[$key3]['name'] = $data_value3['1'];
				$data3[$key3]['dept_name'] = $data_value3['2'];
				$data3[$key3]['send_time'] = $data_value3['0'];
				$data3[$key3]['start_time'] = $data_value3['4'];
				$data3[$key3]['end_time'] = $data_value3['5'];
				$data3[$key3]['yuanyin'] = $data_value3['6'];
			}
		}
		
		$this->assign('datetime',$datetime);
		$this->assign('data',$data3);
		$this->assign("empty", emptyhtml("list"));
		$this->display();
	}
    
    public function _empty($name){
    	$this->emptyaction($name);
    }
}
?>